//
//  ChangePasswordController.m
//  BalanceCar
//
//  Created by 郑信鸿 on 2017/10/10.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "ChangePasswordController.h"
#import "UIView+shadow.h"
#import "SeparateLineView.h"
#import "ByteTool.h"

@interface ChangePasswordController ()

@property(nonatomic, strong)UITextField *nowTextFeild;

@property(nonatomic, strong)UITextField *changeTextFeild;

@property(nonatomic, strong)UITextField *againTextFeild;

@end

@implementation ChangePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = XHLocalizedString(@"Change password");
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor whiteColor];
    [backView addShadowToView];
    [self.view addSubview:backView];
    CGFloat gap = 12;
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_offset(gap);
        make.left.mas_offset(gap);
        make.right.mas_offset(-gap);
        make.height.mas_offset(151);
        
    }];
    self.nowTextFeild = [[UITextField alloc] init];
    [backView addSubview:self.nowTextFeild];
    self.nowTextFeild.placeholder = XHLocalizedString(@"Current password");
    self.nowTextFeild.keyboardType = UIKeyboardTypeNumberPad;
    [self.nowTextFeild mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_offset(15);
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.height.mas_offset(20);
        
    }];
    SeparateLineView *line1 = [[SeparateLineView alloc] init];
    [backView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.nowTextFeild.mas_bottom).offset(15);
        make.left.right.mas_offset(0);
        make.height.mas_offset(0.5);
    }];
    self.changeTextFeild = [[UITextField alloc] init];
    [backView addSubview:self.changeTextFeild];
    self.changeTextFeild.placeholder = XHLocalizedString(@"New password");
    self.changeTextFeild.keyboardType = UIKeyboardTypeNumberPad;
    [self.changeTextFeild mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(line1.mas_bottom).offset(15);
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.height.mas_offset(20);
        
    }];
    SeparateLineView *line2 = [[SeparateLineView alloc] init];
    [backView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.changeTextFeild.mas_bottom).offset(15);
        make.left.right.mas_offset(0);
        make.height.mas_offset(0.5);
    }];
    self.againTextFeild = [[UITextField alloc] init];
    [backView addSubview:self.againTextFeild];
    self.againTextFeild.placeholder = XHLocalizedString(@"Input password again");
    self.againTextFeild.keyboardType = UIKeyboardTypeNumberPad;
    [self.againTextFeild mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(line2.mas_bottom).offset(15);
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.height.mas_offset(20);
        
    }];
    UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:changeBtn];
    changeBtn.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btnColor"]];
    changeBtn.layer.cornerRadius = 4.0;
    [changeBtn addTarget:self action:@selector(changeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [changeBtn setTitle:XHLocalizedString(@"Change password") forState:UIControlStateNormal];
    [changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.top.mas_equalTo(backView.mas_bottom).offset(20);
        make.height.mas_offset(40);
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(travelDetail:) name:kPeripheraDidReadDataNotification object:nil];
}


- (void)changeBtnAction{
   
    if (self.nowTextFeild.text.length != 4 || self.changeTextFeild.text.length !=  4 || self.againTextFeild.text.length !=  4) {
        [LTCSingleHud showMessage:XHLocalizedString(@"Password required for four digits")];
        return;
    }
    if (![self.changeTextFeild.text isEqualToString:self.againTextFeild.text]) {
        [LTCSingleHud showMessage:XHLocalizedString(@"Two password input is not the same")];
        return;
    }
    Byte pwbytes[6] = {};
    [ByteTool hexString:self.nowTextFeild.text pwbytes:pwbytes];
    Byte changepwBytes[6] = {};
    [ByteTool hexString:self.changeTextFeild.text pwbytes:changepwBytes];
    Byte bytes[12] = {};
    for (int i = 0; i < 6; i++ ) {
        
        bytes[i] = pwbytes[i];
    }
    
    for (int i = 0; i < 6; i++ ) {
        
        bytes[i + 6] = changepwBytes[i];
    }
    [BaseSendData sendChangePasswordWithByte:bytes];
  
}


- (void)travelDetail:(NSNotification *)notice{
    
     ResultModel *model = notice.object;
    
    if (model.dataType == ChangePasswordType) {
        
         NSInteger code = [BaseAcceptData feedbackSuccessWithdata:model.data];
        if (code == 1) {
            
            [LTCSingleHud showMessage:XHLocalizedString(@"Change Success")];
            //            [[NSUserDefaults standardUserDefaults] setObject:self.changeTextFeild.text forKey:[BalanceBuleManager shareManager].connectPeripheral.identifier.UUIDString ];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            
            [LTCSingleHud showMessage:XHLocalizedString(@"Change the password failure")];
        }
        
    }
    
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
